#include <JETI_EX_SENSOR.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int adc_key_in  = 0;
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor 
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT; 
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;
}

void setup() {
  JB.JetiBox("Test data","Denis Artru");
  JB.UartInit("TEST NOMS");
  JB.addData("Integer","mm");
  JB.addData("Float","");
  JB.addData(">Float.00<","ABC");
  JB.addData("Float_3d","V");
  JB.addData("+ Denis +"," ");
  JB.addData("+ Artru +"," ");
  lcd.begin(16, 2);
}
int j=0;
int button=0;
int last_button=0;
short valeur1=1;
float valeur2=0.1;
float valeur3=0.01;
float valeur4=0.001;
uint8_t date[3] = {0,0,0};
uint8_t gps[5] = {'N',0,0,0};
char alarm='A';

void loop() {
  button=read_LCD_buttons();
  if (button!=last_button) {
    switch (read_LCD_buttons()) {
    case btnLEFT:
      valeur1--;
      valeur2-=0.1;
      valeur3-=0.01;
      valeur4-=0.001;
      alarm--;
      break;
    case btnRIGHT:
      valeur1++;
      valeur2+=0.1;
      valeur3+=0.01;
      valeur4+=0.001;
      alarm++;
      break;
    case btnDOWN:
      valeur1-=10;
      valeur2-=1;
      valeur3-=0.1;
      valeur4-=0.01;
      break;
    case btnUP:
      valeur1+=10;
      valeur2+=1;
      valeur3+=0.1;
      valeur4+=0.01;
      break;
    case btnSELECT:
      JB.setValueGPS(1,gps);
//      JB.setValueGPS(2,gps);
//      JB.setValueGPS(3,gps);
//      JB.setValueGPS(4,gps);
//      JB.setValueDate(1,date);
//      JB.setValueDate(2,date);
//      JB.setValueDate(3,date);
//      JB.setValueDate(4,date);
//      JB.setValueTime(1,date);
      JB.setValueTime(2,date);
//      JB.setValueTime(3,date);
//      JB.setValueTime(4,date);
//      JB.setValue(1,&valeur1);
//      JB.setValue(2,&valeur2,1);
      JB.setValue(3,&valeur3,2);
//      JB.setValue(4,&valeur4,3);
//      JB.setValue(2,&valeur1);
      JB.setValue(4,&valeur1);
      JB.alarm(alarm);
      break;
    }
    date[0]=(uint8_t)valeur1;
//    gps[0]=(uint8_t)valeur1;
    gps[1]=(uint8_t)valeur1;
//    gps[2]=(uint8_t)valeur1;
//    gps[3]=(uint8_t)valeur1;

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(valeur1,DEC);
    lcd.print(" ");
    lcd.print(valeur2);
    lcd.print(" ");
    lcd.print(alarm);
    lcd.setCursor(0,1);
    lcd.print(valeur2);
    lcd.print(" ");
    lcd.print(valeur3);
    last_button=button;
  }
  JB.createFrame();
  delay(1);
}




